package com.kggs.modbus4jgateway.utils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author : fjc.dane@gmail.com
 * @createtime : 2021/10/27 11:58
 */

public class AIStore {

    /**
     * 创建日志文件
     *
     * @param filenameTemp
     * @return
     * @throws IOException
     */
    private boolean crateFile(String filenameTemp) throws IOException {
        boolean result = false;
        File file = new File(filenameTemp);
        if (!file.exists()) {
            file.createNewFile();
            result = true;
        }
        return result;
    }

    /**
     * 根据AI参数，构建日志内容
     *
     * @param result
     */
    public String getLogs(int[] result) {

        //构建值
        StringBuilder builder = new StringBuilder();
        builder.append("命令是否有效：" + result[0] + " ");
        builder.append("左侧水泵1启动控制：" + result[1] + " ");
        builder.append("右侧水泵1启动控制：" + result[2] + " ");
        builder.append("左侧主机1启动信号：" + result[3] + " ");
        builder.append("右侧主机1启动信号：" + result[4] + " ");
        builder.append("左侧主机1热水模式：" + result[5] + " ");
        builder.append("右侧主机1热水模式：" + result[6] + " ");
        builder.append("左侧主机1风水模式：" + result[7] + " ");
        builder.append("右侧主机1风水模式：" + result[8] + " ");
        builder.append("左侧主机2启动信号：" + result[9] + " ");
        builder.append("右侧主机2启动信号：" + result[10] + " ");
        builder.append("左侧主机2热水模式：" + result[11] + " ");
        builder.append("右侧主机2热水模式：" + result[12] + " ");
        builder.append("左侧主机2风水模式：" + result[13] + " ");
        builder.append("左侧主机2风水模式：" + result[14] + " ");
        builder.append("左侧水泵运转频率：" + result[15] + " ");
        builder.append("右侧水泵运转频率：" + result[16] + " ");
        builder.append("左侧主机1制热启动温度：" + result[17] + " ");
        builder.append("左侧主机1制热停止温度：" + result[18] + " ");
        builder.append("右侧主机1制热启动温度：" + result[19] + " ");
        builder.append("右侧主机1制热停止温度：" + result[20] + " ");
        builder.append("左侧主机2制热启动温度：" + result[21] + " ");
        builder.append("左侧主机2制热停止温度：" + result[22] + " ");
        builder.append("右侧主机2制热启动温度：" + result[23] + " ");
        builder.append("右侧主机2制热停止温度：" + result[24] + " ");
        builder.append("左侧主机1热风启动温度：" + result[25] + " ");
        builder.append("左侧主机1热风停止温度：" + result[26] + " ");
        builder.append("右侧主机1热风启动温度：" + result[27] + " ");
        builder.append("右侧主机1热风停止温度：" + result[28] + " ");
        builder.append("左侧主机2热风启动温度：" + result[29] + " ");
        builder.append("左侧主机2热风停止温度：" + result[30] + " ");
        builder.append("右侧主机2热风启动温度：" + result[31] + " ");
        builder.append("右侧主机2热风停止温度：" + result[32] + " ");

        return builder.toString();
    }

    /**
     * @param filepath
     * @param newstr
     * @return
     * @throws IOException
     */
    private boolean writeFileContent(String filepath, String newstr) throws IOException {
        Boolean bool = false;
        String filein = newstr + "\r\n";//新写入的行，换行
        String temp = "";

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();


            //文件原有内容
            for (int i = 0; (temp = br.readLine()) != null; i++) {
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(filein);

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }

    /**
     * 格式化日期
     * @param date
     * @param fmt
     * @return
     */
    private static String fds(Date date, String fmt) {
        SimpleDateFormat df = new SimpleDateFormat(fmt);
        df.setLenient(false);
        return df.format(date);
    }

    /**
     * @param result
     * @param path
     * @return
     * @throws IOException
     */
    public boolean save(int[] result, String path) throws IOException {
        String filename = fds(new Date(), "yyyy-MM-dd");
        //
        String filenameTemp = path + filename + ".txt";
        this.crateFile(filenameTemp);
        //构建AI日志
        String content = this.getLogs(result);
        return this.writeFileContent(filenameTemp, content);
    }
}
